feat(config): add additionalProperties support to generated config models#5131
Merged
xrmx merged 11 commits intoopen-telemetry:mainfrom Apr 24, 2026
Merged
Conversation
Schema types with additionalProperties (Sampler, SpanExporter, TextMapPropagator, ExperimentalResourceDetector, etc.) now capture unknown keyword arguments in an additional_properties dict field. This enables plugin/custom component names to flow through typed dataclasses without modifying the codegen tool. Implementation: - _additional_properties_support decorator in _common.py wraps the dataclass __init__ to route unknown kwargs into additional_properties - Custom dataclass.jinja2 template for datamodel-codegen conditionally applies the decorator and field when additionalPropertiesType is set - pyproject.toml updated with custom-template-dir and additional-imports - models.py regenerated via tox -e generate-config-from-jsonschema Assisted-by: Claude Opus 4.6
22 tasks
…kwargs - Replace object.__setattr__ with plain self.additional_properties = extra (no frozen dataclasses, simpler and more Pythonic) - Add pylint disable=unexpected-keyword-arg on test lines that intentionally pass unknown kwargs to verify the decorator captures them - Add comment explaining setattr usage for __signature__ (pyright workaround) Assisted-by: Claude Opus 4.6
herin049
reviewed
Apr 21, 2026
herin049
approved these changes
Apr 21, 2026
…perties ClassVar tells type checkers the attribute exists without creating a dataclass field. This means additional_properties doesn't appear in __init__ signature, dataclasses.fields(), asdict(), or repr() — only schema-defined fields show up. The decorator sets it as a plain instance attribute at runtime. Assisted-by: Claude Opus 4.6
…s-support' into mike/config-additional-properties-support
tammy-baylis-swi
approved these changes
Apr 21, 2026
herin049
approved these changes
Apr 22, 2026
Shorter name, aligns with the attribute it creates. Assisted-by: Claude Opus 4.6
…s-support' into mike/config-additional-properties-support
pmcollins
approved these changes
Apr 23, 2026
xrmx
approved these changes
Apr 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds support for JSON Schema
additionalPropertiesto the generated config dataclasses. This is the foundation for plugin/custom component loading in declarative file configuration — unknown component names (e.g.my_custom_sampler,my_custom_exporter) can now flow through typed dataclasses without being silently dropped.Problem
The OTel configuration JSON schema defines
additionalPropertieson 12 types (Sampler,SpanExporter,TextMapPropagator,ExperimentalResourceDetector,LogRecordExporter,PushMetricExporter, etc.) to allow plugin component names as keys. However,datamodel-codegenignoresadditionalPropertieswhen generating plain Python dataclasses — there's no stdlib mechanism for it. Unknown kwargs are rejected by the dataclass__init__.Solution
A custom
datamodel-codegentemplate (opentelemetry-sdk/codegen/dataclass.jinja2) conditionally adds two things to affected classes:@_additional_properties_supportdecorator — wraps the dataclass__init__to route unknown kwargs into anadditional_propertiesinstance attribute instead of raisingTypeErroradditional_properties: ClassVar[dict[str, Any]]annotation — satisfies type checkers without creating a dataclass field, soadditional_propertiesdoesn't appear in__init__signature,dataclasses.fields(),asdict(), orrepr()The decorator preserves the original
inspect.signature()(for IDE autocompletion) and appends**kwargsto signal extras are accepted.How it works
The template checks
additionalPropertiesType, a context variable thatdatamodel-codegensets for schema types whereadditionalPropertiesis a type object (e.g.{"type": ["object", "null"]}). This is how the OTel schema defines it for plugin-capable types.Usage
Codegen
No changes to the codegen workflow —
tox -e generate-config-from-jsonschemapicks up the custom template via thecustom-template-dirconfig inpyproject.toml.Tests
TestAdditionalPropertiesSupport— 5 unit tests for the decorator (known fields, unknown fields, mixed, empty, signature preservation)TestGeneratedModelsHaveAdditionalProperties— 6 regression tests verifying the key generated models accept unknown kwargs (guards againstdatamodel-codegenchanging how it passes the template variable)